package com.yuanye.gwcit.utils;

import org.apache.commons.net.ntp.NTPUDPClient;
import org.apache.commons.net.ntp.TimeInfo;
import org.apache.commons.net.ntp.TimeStamp;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class NTPClient {

    private NTPUDPClient ntpudpClient = null;
    private InetAddress inetAddress = null;
    private boolean isInit = false;
    
    public void initNTPClient(String url, int timeout) {
       this.ntpudpClient = new NTPUDPClient();
       //设置连接超时时间
       this.ntpudpClient.setDefaultTimeout(timeout);
        try {
            this.inetAddress = InetAddress.getByName(url);
            this.isInit = true;
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

    public String getNTPServerTime() {
    	SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (this.isInit) {
            try {
                TimeInfo timeInfo = this.ntpudpClient.getTime(this.inetAddress);
                TimeStamp timeStamp = timeInfo.getMessage().getTransmitTimeStamp();
                Date date = timeStamp.getDate();
                
                return simpleDateFormat.format(date);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Date date = new Date();
        return simpleDateFormat.format(date);
       // return null;
    }

    public long getNTPServerLongTime() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (this.isInit) {
            try {
                TimeInfo timeInfo = this.ntpudpClient.getTime(this.inetAddress);
                TimeStamp timeStamp = timeInfo.getMessage().getTransmitTimeStamp();
                Date date = timeStamp.getDate();

                return date.getTime();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
         return 0;
    }
    
    public String getNTPServerTime(String dateFormat) {
    	SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
        if (this.isInit) {
            try {
                TimeInfo timeInfo = this.ntpudpClient.getTime(this.inetAddress);
                TimeStamp timeStamp = timeInfo.getMessage().getTransmitTimeStamp();
                Date date = timeStamp.getDate();
                
                return simpleDateFormat.format(date);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Date date = new Date();
        return simpleDateFormat.format(date);
       // return null;
    }

    public void stopNTPClient() {
        this.isInit = false;
        if (null != this.ntpudpClient)
            this.ntpudpClient.close();
    }

    
//    public static void main(String[] args) {
//    	NTPClient ntpClient = new NTPClient();
//	    ntpClient.initNTPClient("192.168.31.76", 5000);
//	    String time = ntpClient.getNTPServerTime("YYYYMMdd-HHmmss");
//	    System.out.println("----date server----"+time);
//	}
}
